<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #box{
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script>
        var w=document.documentElement.clientWidth
        var h= document.documentElement.clientHeight
      
        window.onmousedown=function(){
            window.onmousemove=function(e){
                e=e || window.event
                var x=e.clientX-50
                var y=e.clientY-50
                console.log(w,h)
                if(x+100>w){
                    x=w-100
                }else if(x<0){
                    x=0
                }
                if(y+100>h){
                    y=h-100
                }else if(y<0){
                    y=0
                }
                box.style.left=x+"px"
                box.style.top=y+"px"

                            
            }
        }
        window.onmouseup=function(){
            window.onmousemove=null
        }
    </script>
</body>
</html>